<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Perform a regular expression search and replace using callbacks</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.preg-replace-callback-array" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">preg_replace_callback_array</h1>
  <p class="verinfo">(PHP 7)</p><p class="refpurpose"><span class="refname">preg_replace_callback_array</span> &mdash; <span class="dc-title">Perform a regular expression search and replace using callbacks</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.preg-replace-callback-array-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>preg_replace_callback_array</strong></span>
    ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$patterns_and_callbacks</code></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$subject</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$limit</code><span class="initializer"> = -1</span></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter reference">&$count</code></span>
  ]] ) : <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span></div>

  <p class="para rdfs-comment">
   The behavior of this function is similar to
   <span class="function"><a href="preg_replace_callback.html" class="function">preg_replace_callback()</a></span>, except that callbacks are
   executed on a per-pattern basis.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.preg-replace-callback-array-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">patterns_and_callbacks</code></dt>

     <dd>

      <p class="para">
       An associative array mapping patterns (keys) to callbacks (values).
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">subject</code></dt>

     <dd>

      <p class="para">
       The string or an array with strings to search and replace.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">limit</code></dt>

     <dd>

      <p class="para">
       The maximum possible replacements for each pattern in each
       <code class="parameter">subject</code> string. Defaults to
       <em>-1</em> (no limit).
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">count</code></dt>

     <dd>

      <p class="para">
       If specified, this variable will be filled with the number of
       replacements done.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.preg-replace-callback-array-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <span class="function"><strong>preg_replace_callback_array()</strong></span> returns an array if the
   <code class="parameter">subject</code> parameter is an array, or a string
   otherwise. On errors the return value is <strong><code>NULL</code></strong>
  </p>
  <p class="para">
   If matches are found, the new subject will be returned, otherwise
   <code class="parameter">subject</code> will be returned unchanged. 
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.preg-replace-callback-array-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5868">
    <p><strong>Example #1 <span class="function"><strong>preg_replace_callback_array()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$subject&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Aaaaaa&nbsp;Bbb'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">preg_replace_callback_array</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'~[a]+~i'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;function&nbsp;(</span><span style="color: #0000BB">$match</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$match</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]),&nbsp;</span><span style="color: #DD0000">'&nbsp;matches&nbsp;for&nbsp;"a"&nbsp;found'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'~[b]+~i'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;function&nbsp;(</span><span style="color: #0000BB">$match</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$match</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]),&nbsp;</span><span style="color: #DD0000">'&nbsp;matches&nbsp;for&nbsp;"b"&nbsp;found'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;],<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$subject<br /></span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
6 matches for &quot;a&quot; found
3 matches for &quot;b&quot; found
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.preg-replace-callback-array-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><a href="pcre.pattern.html" class="link">PCRE Patterns</a></li>
    <li class="member"><span class="function"><a href="preg_replace_callback.html" class="function" rel="rdfs-seeAlso">preg_replace_callback()</a> - 执行一个正则表达式搜索并且使用一个回调进行替换</span></li>
    <li class="member"><span class="function"><a href="preg_quote.html" class="function" rel="rdfs-seeAlso">preg_quote()</a> - 转义正则表达式字符</span></li>
    <li class="member"><span class="function"><a href="preg_replace.html" class="function" rel="rdfs-seeAlso">preg_replace()</a> - 执行一个正则表达式的搜索和替换</span></li>
    <li class="member"><span class="function"><a href="preg_last_error.html" class="function" rel="rdfs-seeAlso">preg_last_error()</a> - 返回最后一个PCRE正则执行产生的错误代码</span></li>
    <li class="member"><a href="functions.anonymous.html" class="link">Anonymous functions</a></li>
    <li class="member"><a href="language.pseudo-types.html#language.types.callback" class="link">callback</a> 类型的信息</li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>